package com.yt.product.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yt.product.models.bo.ShoppingCartBO;
import com.yt.product.models.dto.ProductInfoDTO;
import com.yt.product.models.service.IProductInfoService;
import com.yt.product.models.vo.ProductInfoVO;
import com.yt.product.models.vo.ShoppingCartAccountVO;
import com.yt.product.models.vo.ShoppingCartVO;
import com.yt.user.models.dto.UserAddressDTO;
import com.yt.user.models.service.IUserAddressService;
import com.yt.user.models.vo.UserAddressVO;
import io.swagger.models.auth.In;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.yt.product.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.product.mapper.ShoppingCartMapper;
import com.yt.product.models.service.IShoppingCartService;
import com.yt.product.models.bo.ShoppingCartSaveBO;
import com.yt.product.models.dto.ShoppingCartDTO;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 购物车控制器
 *
 * @author lvzeming
 * @Date 2025-08-22 15:52:56
 */
@DubboService
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCartDTO> implements IShoppingCartService{

    @Resource
    private YmlConfig ymlConfig;

    @DubboReference
    private IProductInfoService productInfoService;

    @DubboReference
    private IUserAddressService userAddressService;

    @Override
    public boolean add(ShoppingCartSaveBO saveBO) {
        ShoppingCartDTO dto = new ShoppingCartDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public boolean saveCart(ShoppingCartSaveBO saveBO) {


        //判断该用户对应的商品是否已经存在于购物车中
        Wrapper<ShoppingCartDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",saveBO.getUserId());
        wrapper.eq("product_id",saveBO.getProductId());
        ShoppingCartDTO shoppingCartDTO = this.selectOne(wrapper);
        if(null != shoppingCartDTO){
            //数量相加
            Integer count = saveBO.getNum() + shoppingCartDTO.getNum();
            //计算总价
            BigDecimal num = new BigDecimal(count);
            BigDecimal totalPrice = saveBO.getPrice().multiply(num);
            shoppingCartDTO.setTotalPrice(totalPrice);
            shoppingCartDTO.setNum(count);
            return this.updateById(shoppingCartDTO);
        }

        //计算总价
        BigDecimal num = new BigDecimal(saveBO.getNum());
        BigDecimal totalPrice = saveBO.getPrice().multiply(num);
        saveBO.setTotalPrice(totalPrice);

        //保存购物车数据
        return add(saveBO);
    }

    @Override
    public boolean mergeCartList(List<ShoppingCartSaveBO> list) {
        for(ShoppingCartSaveBO shoppingCartSaveBO : list){
            saveCart(shoppingCartSaveBO);
        }
        return true;
    }

    @Override
    public List<ShoppingCartVO> queryCartByUserId(Long userId) {
        Wrapper<ShoppingCartDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",userId);

        List<ShoppingCartVO> shoppingCartVOList = new ArrayList<>();

        List<ShoppingCartDTO> shoppingCartDTOList = this.selectList(wrapper);
        if(null != shoppingCartDTOList && shoppingCartDTOList.size() > 0){
            for(ShoppingCartDTO dto : shoppingCartDTOList){
                ShoppingCartVO shoppingCartVO = new ShoppingCartVO();
                BeanUtils.copyProperties(dto,shoppingCartVO);
                //查询商品详细信息
                ProductInfoDTO productInfoDTO = productInfoService.selectById(dto.getProductId());
                shoppingCartVO.setProductName(productInfoDTO.getProductName());
                shoppingCartVOList.add(shoppingCartVO);
            }
        }

        return shoppingCartVOList;
    }

    @Override
    public ShoppingCartAccountVO getAccountDetail(List<ShoppingCartSaveBO> list) {
        //商品信息
        ShoppingCartAccountVO shoppingCartAccountVO = new ShoppingCartAccountVO();
        List<ShoppingCartVO> shoppingCartVOList = new ArrayList<>();
        int count = 0;
        BigDecimal totalPrice = new BigDecimal(0);
        for(ShoppingCartSaveBO saveBO : list){
            Wrapper<ShoppingCartDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("user_id",saveBO.getUserId());
            wrapper.eq("product_id",saveBO.getProductId());
            ShoppingCartDTO shoppingCartDTO = this.selectOne(wrapper);
            ShoppingCartVO shoppingCartVO = new ShoppingCartVO();
            BeanUtils.copyProperties(shoppingCartDTO,shoppingCartVO);
            shoppingCartVOList.add(shoppingCartVO);

            //计算总件数
            count = count + shoppingCartDTO.getNum();
            //总金额
            totalPrice = totalPrice.add(shoppingCartDTO.getTotalPrice());
        }
        shoppingCartAccountVO.setShoppingCartVOSList(shoppingCartVOList);
        //用户地址信息
        Wrapper<UserAddressDTO> addressDTOWrapper = new EntityWrapper<>();
        addressDTOWrapper.eq("user_id",list.get(0).getUserId());
        List<UserAddressDTO> userAddressDTOList = userAddressService.selectList(addressDTOWrapper);
        List<UserAddressVO> userAddressVOList = new ArrayList<>();
        for(UserAddressDTO userAddressDTO : userAddressDTOList){
            UserAddressVO userAddressVO = new UserAddressVO();
            BeanUtils.copyProperties(userAddressDTO,userAddressVO);
            userAddressVOList.add(userAddressVO);
        }
        shoppingCartAccountVO.setUserAddressVOList(userAddressVOList);

        //统计数据
        BigDecimal postFee = new BigDecimal(5);
        BigDecimal totalPayPrice = totalPrice.subtract(postFee);

        Map<String,Object> summaryMap =  new HashMap<>();
        summaryMap.put("count",count);
        summaryMap.put("totalPrice",totalPrice);
        summaryMap.put("postFee",postFee);
        summaryMap.put("totalPayPrice",totalPayPrice);
        shoppingCartAccountVO.setSummary(summaryMap);




        return shoppingCartAccountVO;
    }
}
